<template>
  <div class="header">
    <div class="header-left">
      <h1 class="page-title">首页</h1>
      <p class="page-subtitle">欢迎使用AI测试自动化平台</p>
    </div>
    <div class="header-right">
      <div class="search-box">
        <i class="fas fa-search search-icon"></i>
        <input 
          type="text" 
          placeholder="搜索功能..." 
          v-model="searchQuery"
          class="search-input"
        >
      </div>
      <button class="upgrade-btn">
        <i class="fas fa-crown upgrade-icon"></i>
        <span>升级到专业版</span>
      </button>
      <div class="contact-author" @mouseenter="showQR = true" @mouseleave="showQR = false">
        <i class="fas fa-user-circle contact-icon"></i>
        <span class="contact-text">联系作者</span>
        <div v-if="showQR" class="qr-tooltip">
          <img src="/picture/bingjiang.png" alt="联系二维码" class="qr-image">
        </div>
      </div>
      <div class="user-menu">
        <img 
          src="/assets/CodeBubbyAssets/2_296/avatar.svg" 
          alt="用户头像" 
          class="user-avatar" 
          @click="toggleUserMenu"
        >
        <div class="user-dropdown" :class="{ show: showUserMenu }">
          <div class="dropdown-header">
            <img src="/assets/CodeBubbyAssets/2_296/avatar.svg" alt="用户头像" class="dropdown-avatar">
            <div class="user-info">
              <div class="user-name">{{ currentUser?.username || 'admin' }}</div>
              <div class="user-email">{{ currentUser?.email || 'admin@aitest.com' }}</div>
            </div>
          </div>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <i class="fas fa-user"></i>
            <span>个人资料</span>
          </a>
          <a href="#" class="dropdown-item">
            <i class="fas fa-cog"></i>
            <span>账户设置</span>
          </a>
          <a href="#" class="dropdown-item">
            <i class="fas fa-question-circle"></i>
            <span>帮助中心</span>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item logout-item" @click="handleLogout">
            <i class="fas fa-sign-out-alt"></i>
            <span>注销</span>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'

export default {
  name: 'Header',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const searchQuery = ref('')
    const showQR = ref(false)
    const showUserMenu = ref(false)

    const currentUser = computed(() => authStore.user)

    const toggleUserMenu = () => {
      showUserMenu.value = !showUserMenu.value
    }

    const handleLogout = () => {
      if (confirm('确定要注销吗？')) {
        authStore.logout()
        router.push('/login')
      }
    }

    const handleClickOutside = (event) => {
      const userMenu = document.querySelector('.user-menu')
      if (userMenu && !userMenu.contains(event.target)) {
        showUserMenu.value = false
      }
    }

    onMounted(() => {
      document.addEventListener('click', handleClickOutside)
    })

    onUnmounted(() => {
      document.removeEventListener('click', handleClickOutside)
    })

    return {
      searchQuery,
      showQR,
      showUserMenu,
      currentUser,
      toggleUserMenu,
      handleLogout
    }
  }
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.header-left h1 {
  font-size: 22px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 4px;
}

.header-left p {
  font-size: 13px;
  color: #64748b;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  color: #94a3b8;
  font-size: 13px;
}

.search-input {
  padding: 8px 12px 8px 35px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 13px;
  width: 200px;
  background-color: #f8fafc;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
  background-color: white;
}

.upgrade-btn {
  display: flex;
  align-items: center;
  padding: 8px 15px;
  background: linear-gradient(135deg, #ffd700 0%, #ffb347 100%);
  color: #333;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(255, 215, 0, 0.3);
  margin-top: -2px;
}

.upgrade-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(255, 215, 0, 0.4);
}

.upgrade-icon {
  margin-right: 6px;
  font-size: 14px;
  color: #ff6b35;
}

.contact-author {
  position: relative;
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: transform 0.2s;
}

.contact-author:hover {
  transform: translateY(-1px);
}

.contact-icon {
  margin-right: 6px;
  font-size: 14px;
}

.qr-tooltip {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  padding: 10px;
  z-index: 100;
}

.qr-image {
  width: 120px;
  height: 120px;
  border-radius: 6px;
}

.user-menu {
  position: relative;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid #e5e7eb;
  cursor: pointer;
  transition: all 0.2s;
}

.user-avatar:hover {
  border-color: #3b82f6;
  transform: scale(1.05);
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  min-width: 220px;
  z-index: 1000;
  display: none;
  border: 1px solid #e5e7eb;
}

.user-dropdown.show {
  display: block;
  animation: dropdownFadeIn 0.2s ease-out;
}

@keyframes dropdownFadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-header {
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.dropdown-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #e5e7eb;
}

.user-info {
  flex: 1;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 2px;
}

.user-email {
  font-size: 12px;
  color: #6b7280;
}

.dropdown-divider {
  height: 1px;
  background: #e5e7eb;
  margin: 0 8px;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: #374151;
  text-decoration: none;
  font-size: 14px;
  transition: background-color 0.2s;
}

.dropdown-item:hover {
  background: #f3f4f6;
}

.dropdown-item i {
  width: 16px;
  font-size: 14px;
  color: #6b7280;
}

.logout-item {
  color: #dc2626;
}

.logout-item:hover {
  background: #fef2f2;
}

.logout-item i {
  color: #dc2626;
}

@media (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .search-input {
    width: 150px;
  }
  
  .contact-text {
    display: none;
  }
}
</style>